﻿using Quartz;
using Stock.Client.Service;
using Stock.Client.Utils;
using Stock.Model.DB;
using Stock.Model.Tencent;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Stock.Client.Schedule
{

      [DisallowConcurrentExecution]
      public class ThimingTask_Tencent : IJob
      {
            public async Task Execute(IJobExecutionContext context)
            {
                  string data = await Tencent_Service.GetApiDataAsync();
                  try
                  {
                        if (!string.IsNullOrEmpty(data))
                        {
                              string[] list = data.Replace("\n", "").Split(';');//处理多个
                              for (int i = 0; i < list.Length; i++)
                              {
                                    if (list[i] == "") continue;
                                    string[] stocks = list[i].Split('~');
                                    StockInfo_Tencent stockInfo_Tencent = new StockInfo_Tencent()
                                    {
                                          name = stocks[1],
                                          code = stocks[2],
                                          current = Convert.ToDecimal(stocks[3]),
                                          last_close = Convert.ToDecimal(stocks[4]),
                                          open = Convert.ToDecimal(stocks[5]),
                                          volume = Convert.ToDecimal(stocks[6]),
                                          outside_dish = Convert.ToInt32(stocks[7]),
                                          inside_dish = Convert.ToInt32(stocks[8]),
                                          buy1 = Convert.ToDecimal(stocks[9]),
                                          buy1_volume = Convert.ToInt32(stocks[10]),
                                          buy2 = Convert.ToDecimal(stocks[11]),
                                          buy2_volume = Convert.ToInt32(stocks[12]),
                                          buy3 = Convert.ToDecimal(stocks[13]),
                                          buy3_volume = Convert.ToInt32(stocks[14]),
                                          buy4 = Convert.ToDecimal(stocks[15]),
                                          buy4_volume = Convert.ToInt32(stocks[16]),
                                          buy5 = Convert.ToDecimal(stocks[17]),
                                          buy5_volume = Convert.ToInt32(stocks[18]),
                                          sale1 = Convert.ToDecimal(stocks[19]),
                                          sale1_volume = Convert.ToInt32(stocks[20]),
                                          sale2 = Convert.ToDecimal(stocks[21]),
                                          sale2_volume = Convert.ToInt32(stocks[22]),
                                          sale3 = Convert.ToDecimal(stocks[23]),
                                          sale3_volume = Convert.ToInt32(stocks[24]),
                                          sale4 = Convert.ToDecimal(stocks[25]),
                                          sale4_volume = Convert.ToInt32(stocks[26]),
                                          sale5 = Convert.ToDecimal(stocks[27]),
                                          sale5_volume = Convert.ToInt32(stocks[28]),
                                          trade_volume =string.IsNullOrEmpty(stocks[29])?0 : Convert.ToInt32(stocks[29]), //最近逐笔成交
                                          trade_date = stocks[30], //时间
                                          chg = Convert.ToDecimal(stocks[31]),
                                          percent = Convert.ToDecimal(stocks[32]),
                                          high = Convert.ToDecimal(stocks[33]),
                                          low = Convert.ToDecimal(stocks[34]),
                                          price_group = stocks[35],
                                          total_volume = Convert.ToInt32(stocks[36]),//总成交量（手）
                                          total_amount = Convert.ToDecimal(stocks[37]),
                                          turnover_rate = Convert.ToDecimal(stocks[38]),  //换手率
                                          PE = Convert.ToDecimal(stocks[39]),
                                          amplitude = Convert.ToDecimal(stocks[43]),//振幅
                                          market_capital = Convert.ToDecimal(stocks[44]),
                                          total_market_capital = Convert.ToDecimal(stocks[45]),
                                          PB = Convert.ToDecimal(stocks[46]),
                                          harden_price = Convert.ToDecimal(stocks[47]),
                                          drop_stop_price = Convert.ToDecimal(stocks[48]),
                                          volume_compare = Convert.ToDecimal(stocks[49]),
                                          avg_price = Convert.ToDecimal(stocks[51]),
                                    };                 
                                     TStockLog stockLog = PublicUtils.CopyProperties<TStockLog, StockInfo_Tencent>(stockInfo_Tencent);
                                    stockLog.CLOSE = stockInfo_Tencent.current;
                                    stockLog.GET_DATE = DateTime.Now;
                                    //存入保存队列,
                                    QueueUtils.AddTStockLogQueue(stockLog);                                   
                              }

                        }
                  }
                  catch (Exception ex)
                  {
                        throw ex;
                  }

            }


      }
}
